var e = require('../@babel/runtime/helpers/objectSpread2.js'),
  t = require('../@babel/runtime/helpers/regeneratorRuntime.js'),
  a = require('../@babel/runtime/helpers/asyncToGenerator.js'),
  o = getApp();
module.exports = Behavior({
  properties: {},
  options: {},
  data: {},
  pageLifetimes: {},
  created: function () {},
  attached: function () {},
  ready: function () {},
  methods: {
    _handleOnline: function (e) {
      var o = this;
      return a(
        t().mark(function a() {
          var n, i, r;
          return t().wrap(
            function (t) {
              for (;;)
                switch ((t.prev = t.next)) {
                  case 0:
                    wx.showLoading({ title: '请稍等片刻', mask: !0 }),
                      (n = null);
                    try {
                      (i = e.currentTarget.dataset),
                        (n = { sn_type: i.type || '', sn_code: i.id || '' });
                    } catch (e) {
                      console.log('kefu behavior 获取参数失败: ', e),
                        o._handleKefuOver(),
                        wx.hideLoading();
                    }
                    return (
                      (r = null), (t.prev = 4), (t.next = 7), o._getImInfo(n)
                    );
                  case 7:
                    (r = t.sent), (t.next = 16);
                    break;
                  case 10:
                    (t.prev = 10),
                      (t.t0 = t.catch(4)),
                      o._handleKefuOver(),
                      wx.hideLoading(),
                      'function' == typeof o._goWebOnlie && o._goWebOnlie(),
                      console.log('请求接口 v1/person/url 出现了问题: ', t.t0);
                  case 16:
                    console.log('imInfo:', r);
                    try {
                      0 === r.switch_im
                        ? ('function' == typeof o._goWebOnlie &&
                            o._goWebOnlie(),
                          console.log(
                            '跳转到老版本客服，如果没有跳转，请检查有没有 _goWebOnlie 方法',
                          ))
                        : (1 !== r.switch_im && 2 !== r.switch_im) ||
                          (o._qyChat(r.switch_im_corpid, r.switch_im_url),
                          console.log('跳转到企业微信客服'));
                    } catch (e) {
                      o._handleKefuOver(),
                        wx.hideLoading(),
                        console.log('天哪，居然走到了这一步，厉害呀~');
                    }
                    'function' == typeof o.handleKefuOver &&
                      o._handleKefuOver(),
                      wx.hideLoading();
                  case 20:
                  case 'end':
                    return t.stop();
                }
            },
            a,
            null,
            [[4, 10]],
          );
        }),
      )();
    },
    _qyChat: function (e, t) {
      wx.canIUse('openCustomerServiceChat')
        ? (o.$api.common.qywechatStat(),
          wx.openCustomerServiceChat({
            extInfo: { url: t },
            corpId: e,
            success: function (e) {},
            fail: function (e) {
              wx.showModal({
                content: '唤起企业微信失败：' + JSON.stringify(e),
                showCancel: !1,
              }),
                console.error('调用企业微信 openCustomerServiceChat fail: ', e);
            },
            complete: function (e) {
              wx.hideLoading();
            },
          }))
        : wx.showModal({
            content:
              '当前微信版本过低，没有办法链接到企业微信客服，请升级微信后尝试',
            showCancel: !1,
            success: function (e) {
              e.confirm &&
                (wx.canIUse('updateWeChatApp') && wx.updateWeChatApp(),
                wx.hideLoading());
            },
          });
    },
    _getWorkTime: function () {
      var e = this,
        t = wx.getStorageSync('initSystem').chat_server_domain_name,
        a = wx.getStorageSync('accessToken'),
        o = wx.getStorageSync('user'),
        n = o.member ? o.member.id : 0,
        i = { Authorization: 'Bearer '.concat(a) },
        r = { company_id: 6, token: a, device_type: 'mp', person_id: n };
      wx.request({
        url: ''.concat(t, '/api/v1/service/work-time'),
        data: r,
        header: i,
        method: 'get',
        success: function (t) {
          0 === t.data.code && 200 === t.data.status
            ? e.setData({
                BEHAVIOR_work_time_words: t.data.data.work_time_words,
              })
            : wx.showModal({
                content: '获取客服时间失败: ' + JSON.stringify(t),
                showCancel: !1,
              });
        },
        fail: function (e) {
          wx.showModal({
            content: '获取客服时间失败: ' + JSON.stringify(e),
            showCancel: !1,
          });
        },
      });
    },
    _getImInfo: function (t) {
      return (
        console.log('_getImInfo in :', t),
        new Promise(function (a, o) {
          console.log('_getImInfo Promise in ');
          var n = wx.getStorageSync('initSystem').chat_server_domain_name,
            i = wx.getStorageSync('accessToken'),
            r = wx.getStorageSync('user'),
            c = r.member ? r.member.id : 0,
            s = { Authorization: 'Bearer '.concat(i) },
            d = e(
              e({}, t),
              {},
              { company_id: 6, token: i, device_type: 'mp', person_id: c },
            );
          wx.request({
            url: ''.concat(n, '/api/v1/person/url'),
            data: d,
            header: s,
            method: 'get',
            success: function (e) {
              if (0 === e.data.code && 200 === e.data.status) {
                var t = {
                  switch_im: e.data.data.switch_im || 0,
                  switch_im_url: e.data.data.hasOwnProperty('switch_im_url')
                    ? e.data.data.switch_im_url
                    : e.data.data.hasOwnProperty('url')
                      ? e.data.data.url
                      : '',
                  switch_im_corpid: e.data.data.hasOwnProperty(
                    'switch_im_corpid',
                  )
                    ? e.data.data.switch_im_corpid
                    : e.data.data.hasOwnProperty('corp_id')
                      ? e.data.data.corp_id
                      : '',
                };
                a(t);
              } else o(e);
            },
            fail: function (e) {
              o(e);
            },
          });
        })
      );
    },
  },
});
